#ifndef NETWORKMANAGER_H
#define NETWORKMANAGER_H

#include <QObject>
#include <QMutex>
#include "networkworker.h"


class NetworkManager : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool serverConnected READ serverConnected NOTIFY serverConnectedChanged)
    Q_PROPERTY(QString currentVersion READ currentVersion NOTIFY currentVersionChanged)
    Q_PROPERTY(QString latestVersion READ latestVersion NOTIFY latestVersionChanged)
    Q_PROPERTY(QString updateContent READ updateContent NOTIFY updateContentChanged)
    Q_PROPERTY(QDateTime updateTime READ updateTime NOTIFY updateTimeChanged)
    Q_PROPERTY(QString deviceName READ deviceName NOTIFY deviceInfoChanged)
    Q_PROPERTY(QDateTime registrationDate READ registrationDate NOTIFY deviceInfoChanged)
    Q_PROPERTY(QString stregistrationDate READ stregistrationDate NOTIFY deviceInfoChanged)
    Q_PROPERTY(QString owner READ owner NOTIFY deviceInfoChanged)
    Q_PROPERTY(QDateTime expiryDate READ expiryDate NOTIFY deviceInfoChanged)
    Q_PROPERTY(QString stexpiryDate READ stexpiryDate NOTIFY deviceInfoChanged)
    Q_PROPERTY(QString endUser READ endUser NOTIFY deviceInfoChanged)
    Q_PROPERTY(QString releasedBy READ releasedBy NOTIFY latestVersionChanged)
    Q_PROPERTY(QString serialnumber READ serialnumber NOTIFY deviceInfoChanged)

public:
    static NetworkManager* instance();


    Q_INVOKABLE void queryMacAddress12Chars();//加载完发
    bool serverConnected() const;
    QString currentVersion() const;
    QString latestVersion() const;
    QString updateContent() const;
    QDateTime updateTime() const;
    QString deviceName() const;
    QDateTime registrationDate() const;
    QString stregistrationDate() const;
    QString owner() const;
    QDateTime expiryDate() const;
    QString stexpiryDate() const;
    QString endUser() const;
    QString releasedBy() const;
    QString serialnumber() const;

public slots:
    void sendRunningStatus();
    void sendCompressedImage(const QString &imagePath);
    void connectToServer();
    void checkForUpdates();
    void getDeviceInfo();
    void downloadUpdate();

signals:
    void serverConnectedChanged();
    void currentVersionChanged();
    void latestVersionChanged();
    void updateContentChanged();
    void updateTimeChanged();
    void deviceInfoChanged();
    void logMessage(const QString &message);

private:
    explicit NetworkManager(QObject *parent = nullptr);
    static NetworkManager *m_instance;
    static QMutex m_mutex;
    QNetworkAccessManager *m_networkManager;
    QByteArray encryptSequence(const QString &sequence);

    QString m_secretKey;
    QString m_uniqueSequence;
    NetworkWorker *m_worker;

};
#endif // NETWORKMANAGER_H
